//
//  DetailChooseCreditVC.m
//  MRBang
//
//  Created by 铅笔 on 15/12/16.
//  Copyright © 2015年 杭州易网新科技. All rights reserved.
//

#import "DetailChooseCreditVC.h"
#import "ApplyForACreditCardVC.h"
#import "LoginViewController.h"

#import "FillInInformationVC.h"

@interface DetailChooseCreditVC ()<UITableViewDataSource,UITableViewDelegate>

{
    UIView *view_ZeroBg;//第一个cell 视图背景
    
    BOOL isOpen;//标记是展开还是 闭合

}

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIWebView *myIntroWebView;

@property (nonatomic,strong) NSDictionary *paras_intro;

@property (nonatomic,strong) NSMutableArray *mutableArray_gift;
@property (nonatomic,strong) NSMutableArray *mutableArray_cardmessage;
@end

@implementation DetailChooseCreditVC

- (NSMutableArray *) mutableArray_gift
{
    if (_mutableArray_gift==nil) {
        _mutableArray_gift = [[NSMutableArray alloc] init];
    }
    return _mutableArray_gift;
}

- (NSMutableArray *) mutableArray_cardmessage
{
    if (_mutableArray_cardmessage==nil) {
        _mutableArray_cardmessage = [[NSMutableArray alloc] init];
    }
    return _mutableArray_cardmessage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"帮我办";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    _mutableArray_gift = [@[@"1",@"2",@"3",@"4"] mutableCopy];
    
    //创建子视图
    [self initWithAllSubviews];
    
    isOpen = NO;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self DataSourceRequestListCreditcardView];
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-60)];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = RXDColor(238, 239, 241);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //退出登录
    UIButton *button_nowApply = [[UIButton alloc] initWithFrame:CGRectMake(20, kScreenHeight-45, kScreenWidth-40, 40)];
    [self.view addSubview:button_nowApply];
    button_nowApply.backgroundColor = RXDColor(255, 118, 112);
    [button_nowApply setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_nowApply setTitle:@"立即申请" forState:UIControlStateNormal];
    button_nowApply.layer.cornerRadius = 3.0;
    button_nowApply.clipsToBounds = YES;
    
    [button_nowApply addTarget:self action:@selector(applyNowAction) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark --- UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mutableArray_gift.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cellDetailChooseCredit";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //横线
    UIView *view_line  = [[UIView alloc] init];
    [cell.contentView addSubview:view_line];
    view_line.backgroundColor = [UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
    
    //label title
    UILabel *label_title = [[UILabel alloc] init];
    [cell.contentView addSubview:label_title];
    label_title.font = [UIFont systemFontOfSize:Font_SIZE_24];
    label_title.textColor = RXDColor(170, 170, 170);
    
    //line second
    UIView *view_lineSecond  = [[UIView alloc] init];
    [cell.contentView addSubview:view_lineSecond];
    view_lineSecond.backgroundColor = COLOR_EVERYVIEWBG;

    if (indexPath.row==0) {
        
        //imageview
        UIImageView *imageView_icon = [[UIImageView alloc] initWithFrame:CGRectMake(44*PROPORTION_WIDTH, 35*PROPORTION_WIDTH, 194*PROPORTION_WIDTH, 120*PROPORTION_WIDTH)];
        [cell.contentView addSubview:imageView_icon];
        imageView_icon.layer.cornerRadius = 5.0;
        imageView_icon.clipsToBounds = YES;

        [imageView_icon sd_setImageWithURL:[NSURL URLWithString:self.model_creadit.img] placeholderImage:nil];
        
        view_line.frame = CGRectMake(0, 195*PROPORTION_WIDTH, kScreenWidth, 1.0);
        //image
        UIImageView *imageView_gift = [[UIImageView alloc] initWithFrame:CGRectMake(34*PROPORTION_WIDTH, 30*PROPORTION_WIDTH+view_line.bottom, 26*PROPORTION_WIDTH, 26*PROPORTION_WIDTH)];
        [cell.contentView addSubview:imageView_gift];
        imageView_gift.image = [UIImage imageNamed:@"gift"];
        
        //label  title
        UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(34*PROPORTION_WIDTH+imageView_icon.right, 36*PROPORTION_WIDTH, 442*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
        [cell.contentView addSubview:label_title];
        label_title.textColor = [BaseViewController colorWithHexString:@"#010101"];
        label_title.text = self.paras_intro[@"name"];
        label_title.font = [UIFont systemFontOfSize:Font_SIZE_30];
        
        //label introduction  detail
        UILabel *label_detail = [[UILabel alloc] initWithFrame:CGRectMake(label_title.x, 15*PROPORTION_WIDTH+label_title.bottom, 442*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
        [cell.contentView addSubview:label_detail];
        label_detail.textColor = [BaseViewController colorWithHexString:@"#666666"];
        label_detail.text = self.paras_intro[@"name"];
        label_detail.font = [UIFont systemFontOfSize:Font_SIZE_24];
        
        //label people  acount have  apply
        UILabel *label_apply = [[UILabel alloc] initWithFrame:CGRectMake(label_title.x, 30*PROPORTION_WIDTH+label_detail.bottom, 442*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
        [cell.contentView addSubview:label_apply];
        label_apply.textColor = [BaseViewController colorWithHexString:@"#666666"];
        
        if (self.paras_intro[@"applycycle"]) {
            label_apply.text = [NSString stringWithFormat:@"%@人本月申请",self.paras_intro[@"applycycle"]];
        }
        else
        {
         label_apply.text = [NSString stringWithFormat:@"0人本月申请"];
        }
        
        label_apply.font = [UIFont systemFontOfSize:Font_SIZE_24];
        
        //label
        UILabel *label_intro = [[UILabel alloc] initWithFrame:CGRectMake(12*PROPORTION_WIDTH+imageView_gift.right, 29*PROPORTION_WIDTH+view_line.bottom, 442*PROPORTION_WIDTH, 28*PROPORTION_WIDTH)];
        [cell.contentView addSubview:label_intro];
        label_intro.textColor = [BaseViewController colorWithHexString:@"#666666"];
    
        if ([self.paras_intro[@"cardyh"] length]!=0) {
            
            NSLog(@"dddd");
            label_intro.text = [self.paras_intro[@"cardyh"] substringToIndex:[self.paras_intro[@"cardyh"] rangeOfString:@"<"].location];
        }
        
        else
        {
            label_intro.text = @"暂无优惠活动";
        }
      
        label_intro.font = [UIFont systemFontOfSize:Font_SIZE_24];
        
        //button arrow
        UIButton *button_rightArrow = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-85*PROPORTION_WIDTH, 20*PROPORTION_WIDTH+view_line.bottom, 85*PROPORTION_WIDTH, 40*PROPORTION_WIDTH)];
        [cell.contentView addSubview:button_rightArrow];
        [button_rightArrow setImage:[UIImage imageNamed:@"downarrow"] forState:UIControlStateNormal];
        [button_rightArrow addTarget:self action:@selector(clickDownArrowActiom) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (indexPath.row==self.mutableArray_gift.count-3) {
        
        //line
        UIView *view_linefirst  = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, 20*PROPORTION_WIDTH)];
        [cell.contentView addSubview:view_linefirst];
        view_linefirst.backgroundColor = COLOR_EVERYVIEWBG;
        
        //second
        view_line.frame = CGRectMake(0,89*PROPORTION_WIDTH, kScreenWidth, 1.0);

        //third
        view_lineSecond.frame = CGRectMake(0,(342-10)*PROPORTION_WIDTH, kScreenWidth, 10*PROPORTION_WIDTH);
        
        //title frame
        label_title.frame = CGRectMake(26*PROPORTION_WIDTH,44*PROPORTION_WIDTH, kScreenWidth-120, 20);
        label_title.text = @"卡特权";
        
        //特权
        UIWebView *webView_privilege = [[UIWebView alloc] init];
        webView_privilege.frame = CGRectMake(10, view_line.bottom, kScreenWidth-20, 262*PROPORTION_WIDTH-view_line.height-view_linefirst.height);
        [cell.contentView addSubview:webView_privilege];
        webView_privilege.backgroundColor = [UIColor whiteColor];
        
        UIFont *fontSize = [UIFont systemFontOfSize:Font_SIZE_24];
        
        if (self.paras_intro[@"cardtq"]) {
            //控制webView的 字体大小
            NSString *BookStr = [NSString stringWithFormat:@"<html> \n"
                                 "<head> \n"
                                 "<style type=\"text/css\"> \n"
                                 "body {margin:0;font-size: %@;color: %@;}\n"
                                 "</style> \n"
                                 "</head> \n"
                                 "<body>%@</body> \n"
                                 "</html>",fontSize,COLOT_TEXTGRAY,self.paras_intro[@"cardtq"]];
            
            [webView_privilege loadHTMLString:BookStr baseURL:nil];
            
            //此处是处理网页黑条条的状况
            webView_privilege.opaque = NO;
            [webView_privilege setScalesPageToFit:YES];
            webView_privilege.backgroundColor = [UIColor clearColor];
        }
    }
    
    if (indexPath.row==self.mutableArray_gift.count-2) {
        
        //second
        view_line.frame = CGRectMake(0,71*PROPORTION_WIDTH, kScreenWidth, 1.0);
        
        //third
        view_lineSecond.frame = CGRectMake(0,320*PROPORTION_WIDTH, kScreenWidth, 10*PROPORTION_WIDTH);
        
        //title frame
        label_title.frame = CGRectMake(26*PROPORTION_WIDTH,25*PROPORTION_WIDTH, kScreenWidth-120, 20);
        label_title.text = @"卡信息";

        NSArray *array_title = @[@"卡币种类型",@"卡组织",@"卡等级",@"币种",@"年费政策"];
        
        for (int i=0; i<5; i++) {
            // 显示label
            UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(35*PROPORTION_WIDTH, 33*PROPORTION_WIDTH*i+100*PROPORTION_WIDTH,120*PROPORTION_WIDTH, 27*PROPORTION_WIDTH)];
            [cell.contentView addSubview:label_title];
            label_title.textColor = RXDColor(170, 170, 170);
            label_title.font = [UIFont systemFontOfSize:Font_SIZE_24];
            label_title.text = array_title[i];
            
            //右侧 显示label
            UILabel *label_titleRight = [[UILabel alloc] init];
            [cell.contentView addSubview:label_titleRight];
            label_titleRight.font = [UIFont systemFontOfSize:Font_SIZE_24];
            label_titleRight.frame = CGRectMake(44*PROPORTION_WIDTH+label_title.right,33*PROPORTION_WIDTH*i+100*PROPORTION_WIDTH, 522*PROPORTION_WIDTH, 27*PROPORTION_WIDTH);
            if (self.mutableArray_cardmessage.count!=0) {
                label_titleRight.text = self.mutableArray_cardmessage[i];
            }
            label_titleRight.numberOfLines = 2;
            [label_titleRight sizeToFit];
        }
   }
    if (indexPath.row==self.mutableArray_gift.count-1)
    {
        //title frame
        label_title.frame = CGRectMake(26*PROPORTION_WIDTH,14*PROPORTION_WIDTH, kScreenWidth-120, 20);
        label_title.text = @"服务";
        
        //服务
        UIWebView *webView_discount = [[UIWebView alloc] init];
        webView_discount.frame = CGRectMake(10, label_title.bottom, kScreenWidth-20, 330*PROPORTION_WIDTH-label_title.height-15);
        [cell.contentView addSubview:webView_discount];
        webView_discount.backgroundColor = [UIColor whiteColor];
        
        UIFont *fontSize = [UIFont systemFontOfSize:Font_SIZE_24];
        
        //控制webView的 字体大小
        NSString *BookStr = [NSString stringWithFormat:@"<html> \n"
                             "<head> \n"
                             "<style type=\"text/css\"> \n"
                             "body {margin:0;font-size: %@;color: %@;}\n"
                             "</style> \n"
                             "</head> \n"
                             "<body>%@</body> \n"
                             "</html>",fontSize,COLOT_TEXTGRAY,self.paras_intro[@"cardinfo"]];
        
        [webView_discount loadHTMLString:BookStr baseURL:nil];
        
        //此处是处理网页黑条条的状况
        webView_discount.opaque = NO;
        [webView_discount setScalesPageToFit:YES];
        webView_discount.backgroundColor = [UIColor clearColor];
        
    }
    if (indexPath.row!=0 && (indexPath.row!=self.mutableArray_gift.count-1) && (indexPath.row!=self.mutableArray_gift.count-2) && (indexPath.row!=self.mutableArray_gift.count-3)) {
        
        //title frame
//        label_title.frame = CGRectMake(26*PROPORTION_WIDTH,44*PROPORTION_WIDTH, kScreenWidth-120, 20);
//        label_title.text = @"优惠";

        //优惠
        UIWebView *webView_service = [[UIWebView alloc] init];
        webView_service.frame = CGRectMake(10, view_line.bottom, kScreenWidth-20, 262*PROPORTION_WIDTH);
        [cell.contentView addSubview:webView_service];
        webView_service.backgroundColor = [UIColor whiteColor];
        
        UIFont *fontSize = [UIFont systemFontOfSize:Font_SIZE_24];
        
        //控制webView的 字体大小
        NSString *BookStr = [NSString stringWithFormat:@"<html> \n"
                             "<head> \n"
                             "<style type=\"text/css\"> \n"
                             "body {margin:0;font-size: %@;color: %@;}\n"
                             "</style> \n"
                             "</head> \n"
                             "<body>%@</body> \n"
                             "</html>",fontSize,COLOT_TEXTGRAY,self.paras_intro[@"cardyh"]];
        
        [webView_service loadHTMLString:BookStr baseURL:nil];
        
        //此处是处理网页黑条条的状况
        webView_service.opaque = NO;
        [webView_service setScalesPageToFit:YES];
        webView_service.backgroundColor = [UIColor clearColor];
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 275*PROPORTION_WIDTH;
    }
    
    if (indexPath.row==self.mutableArray_gift.count-3) {
        return 342*PROPORTION_WIDTH;
    }
    
    if (indexPath.row==self.mutableArray_gift.count-2) {
        return 330*PROPORTION_WIDTH;
    }
     if (indexPath.row==self.mutableArray_gift.count-1) {
         return 330*PROPORTION_WIDTH;
     }
    else
    {
        //礼品
       return 162*PROPORTION_WIDTH;
    }
}

#pragma mark ---点击 下拉按钮
- (void) clickDownArrowActiom
{
    if (isOpen==NO) {
        //插入 行
        NSIndexPath *refreshCell = [NSIndexPath indexPathForRow:1 inSection:0];
        
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:refreshCell,nil];
        
        //同样，将数据加到数据列表中
        
        [self.mutableArray_gift insertObject:@"giftMessage" atIndex:0];
        
        [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView reloadData];
        
        isOpen = YES;
        return;

    }
    if (isOpen==YES) {
    
        [self.mutableArray_gift removeObjectAtIndex:0];
        
        [self.tableView reloadData];
         isOpen = NO;
        return;
    }
}

#pragma mark --- 添加h5页面
- (void) initWithHtmlView
{
    self.myIntroWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,self.tableView.height, kScreenWidth, kScreenHeight-250-55)];
    [self.view addSubview:self.myIntroWebView];
    self.myIntroWebView.backgroundColor = [UIColor whiteColor];
    
    [self.myIntroWebView loadHTMLString:self.paras_intro[@"cardinfo"] baseURL:nil];
}

#pragma mark --- 立即申请
- (void) applyNowAction
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"])
    {
        if (![self.paras_intro[@"cardid"] isKindOfClass:[NSNull class]]) {
            ApplyForACreditCardVC *applyForView = [[ApplyForACreditCardVC alloc] init];
            applyForView.str_lianjie = self.paras_intro[@"cardid"];
            [self.navigationController pushViewController:applyForView animated:YES];
        }
        else
        {
            FillInInformationVC *fillInView = [[FillInInformationVC alloc] init];
            [self.navigationController pushViewController:fillInView animated:YES];
        }
    }
    else
    {
        LoginViewController *longin = [[LoginViewController alloc] init];
        longin.fromeWhere = @"detail";
        [self.navigationController pushViewController:longin animated:YES];
    }
   
}

#pragma mark -- 网络数据请求
- (void) DataSourceRequestListCreditcardView
{
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        NSString *strUrl = [NSString stringWithFormat:@"%@creditcard/card/info",MRAPI];
        
        NSDictionary *paras_list = @{@"creditcardcardid":self.model_creadit.creditcardcardid};
        
        [[HttpRequest shardWebUtil] getNetworkRequestURLString:strUrl parameters:paras_list success:^(id obj) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSLog(@"creditcard/card/get%@",obj);
            
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0) {
                
                self.paras_intro = obj;
                
                [self.tableView reloadData];
                
                [self.mutableArray_cardmessage addObject:obj[@"kadengji"]];
                [self.mutableArray_cardmessage addObject:obj[@"kazuzhi"]];
                [self.mutableArray_cardmessage addObject:obj[@"bizhong"]];
                [self.mutableArray_cardmessage addObject:obj[@"kabizhong"]];
                [self.mutableArray_cardmessage addObject:obj[@"nianfeizhengce"]];
//                [self initWithHtmlView];
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            [BaseViewController showMessage:@"请求超时"];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
